package com.masterclient.app.businessmodule.minemodule.index.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.R;
import com.masterclient.app.modules.aftersale.AfterSaleListActivity;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.modules.order.CustomerOrderActivity;
import com.masterclient.app.widget.MaterialBadgeTextView;

public class PersonOrderListView extends FrameLayout implements View.OnClickListener {

    //全部订单
    private FrameLayout tvAllOrder;
    //待付款
    private LinearLayout llDfk;
    private MaterialBadgeTextView dfkBadge;
    //待发货
    private LinearLayout llDfh;
    private MaterialBadgeTextView dfhBadge;
    //待收货
    private LinearLayout llDsh;
    MaterialBadgeTextView dshBadge;
    //待评价
    private LinearLayout llDpj;
    private MaterialBadgeTextView dpjBadge;
    //售后退款
    private LinearLayout llSh;
    private MaterialBadgeTextView shBadge;

    public PersonOrderListView(@NonNull Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.layout_person_order_list,this);
        initUI();
    }

    public PersonOrderListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_person_order_list,this);
        initUI();
    }

    private void initUI() {
        tvAllOrder = findViewById(R.id.tv_all_order);
        tvAllOrder.setOnClickListener(this);
        llDfk = findViewById(R.id.ll_dfk);
        llDfk.setOnClickListener(this);
        dfkBadge = findViewById(R.id.dfkBadge);
        llDfh = findViewById(R.id.ll_dfh);
        llDfh.setOnClickListener(this);
        dfhBadge = findViewById(R.id.dfhBadge);
        llDsh = findViewById(R.id.ll_dsh);
        llDsh.setOnClickListener(this);
        dshBadge = findViewById(R.id.dshBadge);
        llDpj = findViewById(R.id.ll_dpj);
        llDpj.setOnClickListener(this);
        dpjBadge = findViewById(R.id.dpjBadge);
        llSh = findViewById(R.id.ll_sh);
        llSh.setOnClickListener(this);
        shBadge = findViewById(R.id.shBadge);
    }

    public void initView() {
        setHighLightMode();
        initBadges();
    }

    public void initBadges() {
        dfhBadge.setBadgeCount(0, true);
        dfkBadge.setBadgeCount(0, true);
        dpjBadge.setBadgeCount(0, true);
        dshBadge.setBadgeCount(0, true);
        shBadge.setBadgeCount(0, true);
    }

    private void setHighLightMode() {
        dfhBadge.setHighLightMode();
        dfkBadge.setHighLightMode();
        dshBadge.setHighLightMode();
        dpjBadge.setHighLightMode();
        shBadge.setHighLightMode();
    }

    /**
    * 更新待付款数量
    * */
    public void updateDfkCount(String num) {
        if (Integer.valueOf(num) > 0) {
            dfkBadge.setBadgeCount(Integer.valueOf(num));
        } else {
            dfkBadge.setBadgeCount(0, true);
        }
    }

    /**
     * 更新待发货数量
     * */
    public void updateDfhCount(String num) {
        if (Integer.valueOf(num) > 0) {
            dfhBadge.setBadgeCount(Integer.valueOf(num));
        } else {
            dfhBadge.setBadgeCount(0, true);
        }
    }

    /**
     * 更新待收货数量
     * */
    public void updateDshCount(String num) {
        if (Integer.valueOf(num) > 0) {
            dshBadge.setBadgeCount(Integer.valueOf(num));
        } else {
            dshBadge.setBadgeCount(0, true);
        }
    }

    /**
     * 更新待评价数量
     * */
    public void updateDpjCount(String num) {
        if (Integer.valueOf(num) > 0) {
            dpjBadge.setBadgeCount(Integer.valueOf(num));
        } else {
            dpjBadge.setBadgeCount(0, true);
        }
    }

    /**
     * 更新售后数量
     * */
    public void updateShouhouCount(String num) {
        if (Integer.valueOf(num) > 0) {
            shBadge.setBadgeCount(Integer.valueOf(num));
        } else {
            shBadge.setBadgeCount(0, true);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_all_order) {
            //全部订单
            if (UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(CustomerOrderActivity.class, new String[][]{{"position", "0"}});
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_dfk) {
            //待付款
            if (UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(CustomerOrderActivity.class, new String[][]{{"position", "1"}});
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_dfh) {
            //待发货
            if (UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(CustomerOrderActivity.class, new String[][]{{"position", "2"}});
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_dsh) {
            //待收货
            if (UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(CustomerOrderActivity.class, new String[][]{{"position", "3"}});
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_dpj) {
            //待评价
            if (UserDataManager.getInstance().isSystemLogin()) {
                startActivitryWithBundle(CustomerOrderActivity.class, new String[][]{{"position", "4"}});
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_sh) {
            //售后
            if (UserDataManager.getInstance().isSystemLogin()) {
                getContext().startActivity(new Intent(getContext(), AfterSaleListActivity.class));
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        }
    }

    private void startActivitryWithBundle(@NonNull Class clazz, String[][] val) {
        Intent intent = new Intent(getContext(), clazz);
        Bundle bundle = new Bundle();
        if (val != null) {
            for (String[] aVal : val) {
                bundle.putString(aVal[0], aVal[1]);
            }
            intent.putExtras(bundle);
        }
        getContext().startActivity(intent);
    }
}
